$().ready(function() {
	validateRule();

});

jQuery.validator.addMethod("repeats",function(value, element){
    var returnVal = true;
    var repeatArray = $("#buyNums").val().split(',');
    console.log('repeatArray='+value)
    for(var i=0;i<repeatArray.length;i++){
        if($("#buyNum").val() == repeatArray[i]){
            console.log('$("#buyNum")='+$("#buyNum").val())
            returnVal = false;
            return false;//只要发现已经填写过就提示
        }
    }
    return returnVal;
},"数量填写重复");         //验证错误信息

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
    var buyNum = $("#buyNum").val();
    var giftNum = $("#giftNum").val();
	parent.setGift(buyNum,giftNum);
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
            buyNum : {
				required : true,
                digits:true,
                min:1,
                repeats: $("#buyNums").val(),
                max:999999
			},
            giftNum : {
                required : true,
                digits:true,
                min:1,
                max:999999
            },
		},
		messages : {
            buyNum : {
				required : icon + "必填且必须是数字",
                digits: icon + "请输入整数",
                repeats: icon + "数量填写重复，请重新输入",
                min: icon +"所填数字必须大于0"
			},
            giftNum : {
                required : icon + "必填且必须是数字",
                digits: icon + "请输入整数",
                min: icon +"所填数字必须大于0"
            }
		}
	})
}